/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import com.openbrew.openbrewrf.PSOLines;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import tools.InventoryModifiers;

/**
 *
 * @author chapmand
 */
@Stateless
@Path("com.openbrew.openbrewrf.psolines")
public class PSOLinesFacadeREST extends AbstractFacade<PSOLines>  {
    @PersistenceContext(unitName = "com.openBrew_openBrewRF_war_1.0PU")
    private EntityManager em;
    SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");

    public PSOLinesFacadeREST() {
        super(PSOLines.class);
    }

    @POST
    @Override
    @WebMethod(operationName = "create")
    @Consumes({"application/xml", "application/json"})
    public PSOLines create(@WebParam(name = "psolines") PSOLines entity) {
        entity = super.create(entity);
        return entity;
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(PSOLines entity) {
        final InventoryModifiers im = new InventoryModifiers();
        PSOLines oldLine = super.find(entity.getPSOID());
        final int oldQty = oldLine.getPSOQty();
        super.edit(entity);
        final int newQty = entity.getPSOQty();
        if((oldQty - newQty)>0){
            im.doFGIAllocationSubtraction(em, (int)oldLine.getPSOFGI().getFgiid()
                    , oldQty - newQty);
        } else {
            im.doFGIAllocationAddition(em, (int)oldLine.getPSOFGI().getFgiid()
                    , newQty - oldQty);
        }
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        final InventoryModifiers im = new InventoryModifiers();
        PSOLines psoLine = super.find(id);
        super.remove(psoLine);
        im.doFGIAllocationSubtraction(em, (int)psoLine.getPSOFGI().getFgiid()
                , psoLine.getPSOQty());
        em.getEntityManagerFactory().getCache().evictAll();
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public PSOLines find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<PSOLines> findAll() {
        return super.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<PSOLines> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
}
